﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProjectEulerSolutions
{
    /*
     * The Fibonacci sequence is defined by the recurrence relation:

    Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1.

Hence the first 12 terms will be:

    F1 = 1
    F2 = 1
    F3 = 2
    F4 = 3
    F5 = 5
    F6 = 8
    F7 = 13
    F8 = 21
    F9 = 34
    F10 = 55
    F11 = 89
    F12 = 144

The 12th term, F12, is the first term to contain three digits.

What is the first term in the Fibonacci sequence to contain 1000 digits?

     * 
     * 
     * */
    class Problem25
    {
        public static string Calculate()
        {
            int limit = 1000;


            int[] table1 = new int[limit];
            int[] table2 = new int[limit];

            table1[0] = 1;
            table2[0] = 1;
            int term = 2;

            while (true)
            {
                term++;
                int overflow = 0;
                for (int i = 0; i < table1.Length; i++)
                {
                    int num = table2[i] + table1[i] + overflow;
                    table1[i] = num % 10;
                    overflow = num / 10;
                }
                if (table1[table1.Length - 1] != 0)
                {
                    for (int j = table1.Length - 1; j>=0; j--)
                        Console.Write(table1[j]);

                    Console.WriteLine();
                    break;
                }

                term++;
                overflow = 0;
                for (int i = 0; i < table1.Length; i++)
                {
                    int num = table2[i] + table1[i] + overflow;
                    table2[i] = num % 10;
                    overflow = num / 10;
                }
                if (table2[table2.Length - 1] != 0)
                {
                    for (int j = table2.Length - 1; j >= 0; j--)
                        Console.Write(table2[j]);

                    Console.WriteLine();
                    break;
                }
            }



            string result = term.ToString();

            return result;
        }
    }
}
